package com.aat.dxfy.test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Small MongoDB application that uses spring data to interact with MongoDB.
 * 
 * @author Jeroen Reijn
 */
public class MongoDBApp {

    static final Logger logger = LoggerFactory.getLogger(MongoDBApp.class);
	private static ConfigurableApplicationContext context;

    public static void main( String[] args ) {
		logger.info("Bootstrapping MongoDemo application");

		context = new ClassPathXmlApplicationContext("applicationContext.xml");

        PersonRepository personRepository = context.getBean(PersonRepository.class);

        // 清空数据
        personRepository.dropPersonCollection();

        //创建集合
        personRepository.createPersonCollection();

        for(int i=0; i<20; i++) {
        	//插入数据
            personRepository.insertPersonWithNameJohnAndRandomAge();
        }
        
        //查询所有数据
        personRepository.logAllPersons();
        //平均计算
        int avgaGE = personRepository.getAvarageAgeOfPerson();
        logger.info("Avarage age of a person is: {}", avgaGE);

        logger.info("Finished MongoDemo application");
	}
}